﻿using System.Drawing;
using Map;

namespace Map_Creator.MapInfo
{
    public class ObjectLayer : ObjectLayerStruct
    {
        public ObjectLayer(int ID = 0) : base(ID)
        {
        }

        public Map AssociatedMap
        {
            get { return (Map)Map; }
            set { Map = value; }
        }

        public ExtendedTreeNode Node;

        public ObjectData this[int X, int Y]
        {
            get { return Data[X, Y]; }
            set { Data[X, Y] = value; }
        }

        public ObjectData this[Point pos]
        {
            get { return Data[pos.X, pos.Y]; }
            set { Data[pos.X, pos.Y] = value; }
        }

        public override void Delete()
        {
            if (AssociatedMap == null) return;

            AssociatedMap.ObjectLayers.Remove(this);

            AssociatedMap.ObjectLayerCount--;

            if (AssociatedMap.CurrentObjectLayer == this)
                AssociatedMap.CurrentObjectLayer = null;

            var parent = Node.Parent;
            Node.Remove();
            if (parent.Nodes.Count == 0)
                parent.Remove();

            // set null associated map so remove wont ever happen twice :O
            AssociatedMap = null;

            base.Delete();
        }
    }
}
